﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTAccess.Business;
using DCTLib.Common;
using DCTAccess.Business.Member;
using DCTAccess.Business.TransactionLog;
using DCTAccess.Business.Property;

public partial class AdminZone_MemberDetails : System.Web.UI.Page
{
    #region Properties
    string iUnique = string.Empty;

    protected int id;
    public string UserName
    {
        get { return txtUsername.Text; }
        set { txtUsername.Text = value; }
    }

    private string SaveChangeMessage
    {
        get { return lblSaveChange.Text; }
        set { lblSaveChange.Text = value; }
    }

    public string FullName
    {
        get { return txtFullName.Text; }
        set { txtFullName.Text = value; }
    }

    public string LastLogin
    {
        get { return lblLastLogin.Text; }
        set { lblLastLogin.Text = value; }
    }

    public string Birthday
    {
        get { return txtBirthday.Text; }
        set { txtBirthday.Text = value; }
    }

    public string RegDate
    {
        get { return txtRegDate.Text; }
        set { txtRegDate.Text = value; }
    }

    public string Mobile
    {
        get { return txtMobile.Text; }
        set { txtMobile.Text = value; }
    }

    public string Address
    {
        get { return txtAddress.Text; }
        set { txtAddress.Text = value; }
    }

    public string TotalPoint
    {
        get { return txtTotalPoint.Text; }
        set { txtTotalPoint.Text = value; }
    }

    public string MemberRef
    {
        get { return txtTotalMemberRef.Text; }
        set { txtTotalMemberRef.Text = value; }
    }

    public string RefCode
    {
        get { return txtRefCode.Text; }
        set { txtRefCode.Text = value; }
    }
    #endregion

    public string Message
    {
        get { return lblUpdate.Text; }
        set { lblUpdate.Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            id = AppRequestInfo.ID;
            if (id != -1)
            {
                DataTable dtTrans = MemTransaction.GetAllTransaction(id);
                Grid1.DataSource = dtTrans;
                Grid1.DataBind();

                lblFullName.Text = id.ToString();
                Member member = MemberController.GetMemberByID(id);
                if (member != null)
                {
                    UserName = member.UserName;
                    FullName = member.FullName;
                    LastLogin = member.LastLoginDate;
                    Birthday = member.Birthday;
                    RegDate = member.RegDate;
                    Mobile = member.Mobile;
                    Address = member.Address;
                    MemberRef = MemTransaction.SumOfUser(member.UserName);

                    TotalPoint = MemTransaction.PointOfUser(member.UserName);
                    RefCode = GetUniqueCode(member.UniqueCode);

                }
            }
        }
        catch (System.Exception ex)
        {
            return;
        }
    }

    public string GetUniqueCode(string unique)
    {
        if (unique.Contains('|'))
        {
            iUnique = unique.Substring(unique.LastIndexOf('|') + 1);
            return iUnique;
        }
        else
        {
            return unique;
        }
    }

    public bool ValidateData()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtOldPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("Old Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtNewPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("New Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtReNewPassword.Text.Trim()))
        {
            Message += AppMessages.RequireField("Confirm New Password") + "<br>";
            validated = false;
        }
        else if (txtNewPassword.Text.Trim() != txtReNewPassword.Text.Trim())
        {
            Message += AppMessages.MustMatch("New Password", "Confirm New Password") + "<br>";
            validated = false;
        }
        return validated;
    }

    protected void btnSavePass_Click(object sender, EventArgs e)
    {
        if (ValidateData())
        {
            //int id = 
            Member mem = GetMemberForchangePass();
            Member memcm = MemberController.GetMemberByID(mem.ID);

            if (MD5Helper.GetMD5Hash(txtOldPassword.Text) == memcm.HashedPassword)
            {
                if (MemberController.ChangePassword(mem))
                {
                    Message = AppMessages.UPDATE_SUCCESSFULLY;
                }
                else
                {
                    Message = AppMessages.CANNOT_UPDATE;
                }
            }
            else
            {
                Message = AppMessages.NOT_CORRECT_OLD_PASSWORD;
            }
        }
    }

    private Member GetMemberForchangeSavechange()
    {
        Member mem = new Member();
        mem.ID = AppRequestInfo.ID;
        mem.FullName = txtFullName.Text.Trim();
        mem.Mobile = txtMobile.Text.Trim();
        mem.Address = txtAddress.Text.Trim();
        return mem;
    }

    private Member GetMemberForchangePass()
    {
        Member mem = new Member();
        int id = AppRequestInfo.ID;
        mem.ID = id;
        mem.HashedPassword = MD5Helper.GetMD5Hash(txtNewPassword.Text);

        return mem;
    }

    protected void btnClearPass_Click(object sender, EventArgs e)
    {
        txtOldPassword.Text = "";
        txtNewPassword.Text = "";
        txtReNewPassword.Text = "";
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Member mem = GetMemberForchangeSavechange();
        if (MemberController.Update(mem))
        {
            SaveChangeMessage = AppMessages.UPDATE_SUCCESSFULLY;
        }
        else
        {
            SaveChangeMessage = AppMessages.CANNOT_UPDATE;
        }
    }

    protected void btnClearInfo_Click(object sender, EventArgs e)
    {
        //Nothing to do
    }
}
